﻿Public Class DelegateCommand(Of T)
    Implements ICommand

    Private ReadOnly _canExecute As Predicate(Of T)
    Private ReadOnly _execute As Action(Of T)

    Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged

    Public Sub New(ByVal execute As Action(Of T))
        Me.New(execute, Nothing)
    End Sub
    Public Sub New(ByVal execute As Action(Of T), ByVal canExecute As Predicate(Of T))
        _execute = execute
        _canExecute = canExecute
    End Sub

    Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        If _canExecute Is Nothing Then
            Return True
        End If
        Return _canExecute(parameter)
    End Function
    Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
        _execute(parameter)
    End Sub
    Public Sub RaiseCanExecuteChanged()
        If Not CanExecuteChangedEvent Is Nothing Then
            RaiseEvent CanExecuteChanged(Me, EventArgs.Empty)
        End If
    End Sub
End Class
